package com.epam.edu.task03.parser;

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.LinkedList;
import java.util.List;

import com.epam.edu.task03.dances.*;

/**
 * Created with IntelliJ IDEA.
 * User: Zhanna_Mukanova
 * Date: 9/17/13
 * Time: 3:49 PM
 * To change this template use File | Settings | File Templates.
 */
public class SAXHandler extends DefaultHandler {
    private List<Dance> dances = null;
    private List<Dancer> dancers = null;
    private Dance dance = null;
    private Dancer dancer = null;
    private boolean isDanceType, isScene, isNumberOfDancers, isMusic, isNumber, isDancerName, isNumberOfYears;

    public List<Dance> getDances() {
        if (dances == null) {
            dances = new LinkedList<Dance>();
        }
        return this.dances;
    }


    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("dan:dances")) {
            dances = new LinkedList<Dance>();
        } else if (qName.equalsIgnoreCase("dan:dance")) {
            String danceName = attributes.getValue("danceName");
            dance = new Dance();
            dance.setDanceName(danceName);
        } else if (qName.equalsIgnoreCase("dan:type")) {
            isDanceType = true;
        } else if (qName.equalsIgnoreCase("dan:scene")) {
            isScene = true;
        } else if (qName.equalsIgnoreCase("dan:numberOfDancers")) {
            isNumberOfDancers = true;
        } else if (qName.equalsIgnoreCase("dan:music")) {
            isMusic = true;
        } else if (qName.equalsIgnoreCase("dan:number")) {
            isNumber = true;
        } else if (qName.equalsIgnoreCase("dan:dancers")) {
            dancers = new LinkedList<Dancer>();
        } else if (qName.equalsIgnoreCase("dan:dancer")) {
            dancer = new Dancer();
        } else if (qName.equalsIgnoreCase("dan:dancerName")) {
            isDancerName = true;
        } else if (qName.equalsIgnoreCase("dan:numberOfYears")) {
            isNumberOfYears = true;
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("dan:dance")) {
            dances.add(dance);
        } else if (qName.equalsIgnoreCase("dan:dancer")) {
            dancers.add(dancer);
        } else if (qName.equalsIgnoreCase("dan:dancers")) {
            dance.setDancers(dancers);
        }
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        if (isDanceType) {
            dance.setType(DanceType.fromValue(new String(ch, start, length)));
            isDanceType = false;
        } else if (isScene) {
            dance.setScene(SceneType.fromValue(new String(ch, start, length)));
            isScene = false;
        } else if (isNumberOfDancers) {
            dance.setNumberOfDancers(NumberOfDancersType.fromValue(new String(ch, start, length)));
            isNumberOfDancers = false;
        } else if (isMusic) {
            dance.setMusic(MusicType.fromValue(new String(ch, start, length)));
            isMusic = false;
        } else if (isNumber) {
            dance.setNumber(Integer.parseInt(new String(ch, start, length)));
            isNumber = false;
        } else if (isDancerName) {
            dancer.setDancerName(new String(ch, start, length));
            isDancerName = false;
        } else if (isNumberOfYears) {
            dancer.setNumberOfYears(Integer.parseInt(new String(ch, start, length)));
            isNumberOfYears = false;
        }
    }
}
